package xyz.sicau.bootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


// config中
/**
 * 整合SpringBoot 设置延迟队列
 * 两个交换机
 *   一个普通交换机连接两个延迟队列，分别延迟10S和40S
 *   一个死信交换机连接一个死信队列
 */
@Configuration
public class TtlQueueConfig {
    public static final String X_EXCHANGE = "X"; // 延迟交换机，连接两个延迟交换机
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y"; // 死信交换机
    public static final String QUEUE_A = "QA"; // 10s的延迟队列
    public static final String QUEUE_B = "QB"; // 40s的延迟队列
    public static final String QUEUE_C = "QC"; // 这个队列用于发布者来指定过期时间
    public static final String DEAD_LETTER_QUEUE = "QD"; // 死信队列，连接消费者


    // 两个交换机 exchangeX，exchangeY
    @Bean // 参数可以加名字，默认方法名
    public DirectExchange xExchange() {
        // 直接创建的直接交换机
        return new DirectExchange(X_EXCHANGE);
    }

    @Bean
    public DirectExchange yExchange() {
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    // 三个队列 queueA,queueB,queueD
    @Bean
    public Queue queueA() {
        Map<String,Object> args = new HashMap<>(10);
        args.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE); // 指向死信交换机
        args.put("x-dead-letter-routing-key","YD"); // 死信交换机是直接交换需要routingKey
        args.put("x-message-ttl",10000); // 延迟时间，ms
        // durable持久化，withArguments携带的参数
        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();
    }

    @Bean
    public Queue queueB() {
        Map<String,Object> args = new HashMap<>(10);
        args.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key","YD");
        args.put("x-message-ttl",40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();
    }

    @Bean // 发布者定义延迟的队列，就不写ttl
    public Queue queueC() {
        Map<String,Object> args = new HashMap<>(10);
        args.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key","YD");
        return QueueBuilder.durable(QUEUE_C).withArguments(args).build();
    }

    @Bean
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    // 设置绑定关系
    @Bean // eX --XA--> qA  这里直接注入的
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA, @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }
    @Bean // eX --XB--> qB
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB, @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }
    @Bean // eY --YD--> qD
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD, @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
    @Bean // eY --YD--> qD
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC, @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }

}